package com.central.apps.controller;

import com.central.app.service.HandlerService;
import com.central.apps.entity.BlPm;
import com.central.apps.service.OverhaulMgService;
import com.central.apps.service.PmService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@Api(tags = "测试controller")
@RequestMapping("/OverHaul111")
public class OverHaulMgController {

    @Autowired
    private OverhaulMgService OverhaulMgService;

    @Autowired private HandlerService handlerService;

    @Autowired private PmService PmService;

    @PostMapping("/InsertLx")
    public String InsertLx(@RequestBody JSONObject insae) {
        //log.info("===================wonum: "+insae.get("insaenum")+"========="+insae.get("wonum"));
        String wonum = (String) insae.get("wonum");
        String insaenum = (String) insae.get("insaenum");
        //OverhaulMgService.insertLxToWorkoder(wonum,insaenum);

        return  OverhaulMgService.insertLxToWorkoder(wonum,insaenum);
    }

    @PostMapping("/InsertTask")
    public String InsertTask(@RequestBody JSONObject insae) {
        //log.info("===================wonum: "+insae.get("insaenum")+"========="+insae.get("wonum"));
        String wonum = (String) insae.get("wonum");
        String insaenum = (String) insae.get("insaenum");
        //OverhaulMgService.insertLxToWorkoder(wonum,insaenum);



        return  OverhaulMgService.insertTaskToWorkoder(wonum,insaenum);
    }

    @PostMapping("/InsertPmTask")
    @Transactional
    public String InsertPmTask(@RequestBody JSONObject insae) {
        //log.info("===================wonum: "+insae.get("insaenum")+"========="+insae.get("wonum"));
        String wonum = (String) insae.get("wonum");
        String insaenum = (String) insae.get("insaenum");
        //OverhaulMgService.insertLxToWorkoder(wonum,insaenum);
        String getInfosSql ="select * from bl_jobtask where jpnum ='"+insaenum+"'";
        List<Map<String, Object>> getTaskLists = handlerService.findBySql(getInfosSql);

        String insertPmTaskSql = "";

        for (Map<String, Object> taskList : getTaskLists) {
            insertPmTaskSql = "insert into bl_workordertask(inspectionassetnum,inspectioncontent,wonum)values ('"+taskList.get("step")+"','"+taskList.get("workmethod")+"','"+wonum+"')";
            handlerService.updateBySql(insertPmTaskSql);
            log.info("====================="+taskList.get("workmethod"));
        }

        String getWpmaterialSql ="select * from bl_wpmaterial where JOBPLANID ='"+insaenum+"'";
        List<Map<String, Object>> getWpmaterialLists = handlerService.findBySql(getWpmaterialSql);
        log.info("====================="+getWpmaterialLists.size());
        String insertPmwpmaterialSql = "";
        for (Map<String, Object> wpmaterialList : getWpmaterialLists) {
            insertPmwpmaterialSql = "insert into bl_wpmaterial(itemnum,materialEnity_DESCRIPTION,itemqty,wonum)values ('"+wpmaterialList.get("ITEMNUM")+"','"+wpmaterialList.get("DESCRIPTION")+"',"+wpmaterialList.get("ITEMQTY")+",'"+wonum+"')";

            log.info("====================="+wpmaterialList.toString());

            log.info("====================='"+wpmaterialList.get("ITEMNUM")+"','"+wpmaterialList.get("DESCRIPTION")+"',"+wpmaterialList.get("ITEMQTY")+");");

            handlerService.updateBySql(insertPmwpmaterialSql);
        }
        String getWplaborSql ="select * from bl_wplabor where JOBPLANID ='"+insaenum+"'";
        List<Map<String, Object>> getWplaborLists = handlerService.findBySql(getWplaborSql);
        String insertPmWplaborSql = "";
        for (Map<String, Object> WplaborList : getWplaborLists) {
            insertPmWplaborSql = "insert into bl_wplabor(LABORCODE,planUserEnity_DISPLAYNAME,wonum)values('"+WplaborList.get("LABORCODE")+"','"+WplaborList.get("planUserEnity_DISPLAYNAME")+"','"+wonum+"')";
            handlerService.updateBySql(insertPmWplaborSql);
        }


       com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();
        json.put("code", "0");
        json.put("msg", "插入作业标准信息完成！");
        return json.toJSONString();
    }

    @PostMapping("/PmCreateWorkorder")
    public String PmCreateWorkorder(@RequestBody JSONObject PmInfo) throws Exception {
        log.info("===================wonum: "+PmInfo.get("insaenum")+"========="+PmInfo.get("wonum"));
        BlPm blPm = new BlPm();
        JSONObject Pm = (JSONObject) PmInfo.get("PmInfo");
        log.info("================================PmInfo"+Pm.toString());
        log.info("================================"+Pm.get("id"));

        int id = (int) Pm.get("id");
        blPm.setId(id);
        blPm.setInspectionplan((String) Pm.get("inspectionplan"));
        blPm.setInspectionplandesc((String) Pm.get("inspectionplandesc"));
        blPm.setPmnum((String) Pm.get("pmnum"));
        blPm.setDescription((String) Pm.get("description"));
        blPm.setAssetnum((String) Pm.get("assetnum"));
        blPm.setAssetdesc((String) Pm.get("assetdesc"));
        blPm.setLocation((String) Pm.get("location"));
        blPm.setLocationdesc((String) Pm.get("locationdesc"));
        blPm.setLinerepair((String) Pm.get("linerepair"));
        blPm.setLinerepairdesc((String) Pm.get("linerepairdesc"));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date = new Date(sdf.parse((String) Pm.get("nextexecutetime")).getTime());
        log.info("=================================Pm.get(\"nextexecutetime\")"+Pm.get("nextexecutetime"));
        Timestamp Timestamp = new Timestamp(date.getTime());
        //  周期性设置
        blPm.setIsbefore((String) Pm.get("isbefore"));
        blPm.setZ1((String) Pm.get("Z1"));
        blPm.setZ2((String) Pm.get("Z2"));
        blPm.setZ3((String) Pm.get("Z3"));
        blPm.setZ4((String) Pm.get("Z4"));
        blPm.setZ5((String) Pm.get("Z5"));
        blPm.setZ6((String) Pm.get("Z6"));
        blPm.setZ7((String) Pm.get("Z7"));
        blPm.setIsoncyel((String) Pm.get("isoncyel"));
        blPm.setIsskipweek((String) Pm.get("isskipweek"));
        blPm.setClasscode((String) Pm.get("classcode"));
        blPm.setClassdesc((String) Pm.get("classdesc"));
        blPm.setFzrdesc((String) Pm.get("fzrdesc"));
        blPm.setPerson((String) Pm.get("person"));
        blPm.setDeptcode((String) Pm.get("deptcode"));
        blPm.setSitecode((String) Pm.get("sitecode"));
        blPm.setOrgcode((String) Pm.get("orgcode"));

        blPm.setNextexecutetime(Timestamp);
        log.info("==========================frequency"+Pm.get("frequency"));
        blPm.setFrequency(Pm.get("frequency")+"");
        blPm.setFrequencyunit((String) Pm.get("frequencyunit"));
        blPm.setIsaction(Pm.getString("isaction"));

        return PmService.PmCreateWorkorder(blPm);
    }

    @PostMapping("/test")
    public String test2() throws Exception {

       return OverhaulMgService.TestCreate();
    }
}
